#include <assert.h>
#include "storeconfig.h"


static shooter_ptr g_pShooter = NULL;
static target_ptr g_pTarget = NULL;
extern guiimport_t g_gui_imp;

shooter_ptr GetShooterPtr(void)
{
	if (NULL == g_pShooter)
	{
		g_pShooter = (shooter_ptr)Call_(g_gui_imp.zm, sizeof(shooter_t));
		g_pShooter->pDate = (date_ptr)Call_(g_gui_imp.zm, sizeof(date_t));
	}

	return g_pShooter;
}

void DropShooterPtr(void)
{
	if (NULL != g_pShooter)
	{
		Call_(g_gui_imp.zf, g_pShooter->pDate);
		Call_(g_gui_imp.zf, g_pShooter);
		g_pShooter = NULL;
	}
}

target_ptr GetTargetPtr(void)
{
	if (NULL == g_pTarget)
	{
		g_pTarget = (target_ptr)Call_(g_gui_imp.zm, sizeof(target_t));
	}

	return g_pTarget;
}

void DropTargetPtr(void)
{
	if (NULL != g_pTarget)
	{
		Call_(g_gui_imp.zf, g_pTarget);
		g_pTarget = NULL;
	}
}

/* assume that incoming pointer is non-null and local pointer of 
	global object is also non-null, or report an error */
void GetConfig(shooter_ptr pShooter, target_ptr pTarget)
{
	shooter_ptr pShooter2 = NULL;
	target_ptr pTarget2 = NULL;

	assert(NULL != pShooter);
	assert(NULL != pTarget);

	pShooter2 = GetShooterPtr();
	strcpy(pShooter->name, pShooter2->name);
	memcpy(pShooter->pDate, pShooter2->pDate, sizeof(date_t));
	pShooter->trainMode = pShooter2->trainMode;
	pShooter->techType = pShooter2->techType;
	pShooter->dInitSTDist = pShooter2->dInitSTDist;
	pShooter->nMagRate = pShooter2->nMagRate;

	pTarget2 = GetTargetPtr();
	pTarget->nVelocity = pTarget2->nVelocity;
}

void SetShooter(char* pName, date_ptr pDate, TRAINMODEENUM mode)
{
	g_pShooter = GetShooterPtr();
	strcpy(g_pShooter->name, pName);
	memcpy(g_pShooter->pDate, pDate, sizeof(date_t));
	g_pShooter->trainMode = mode;

	g_gui_imp.Sys_Printf("name:\t%s\nyear:\t%d\nmonth:\t%d\nday:\t%d\nmode:\t%d\n",
						g_pShooter->name, g_pShooter->pDate->year,
						g_pShooter->pDate->month, g_pShooter->pDate->day,
						g_pShooter->trainMode);
}

void SetShooter2(TECHNIQUETYPE type, double dist, unsigned int rate)
{
	g_pShooter = GetShooterPtr();
	g_pShooter->techType = type;
	g_pShooter->dInitSTDist = dist;
	g_pShooter->nMagRate = rate;

	g_gui_imp.Sys_Printf("type:\t%d\ndist:\t%f\nrate:\t%d\n",
						g_pShooter->techType, g_pShooter->dInitSTDist,
						g_pShooter->nMagRate);
}

void SetTarget(unsigned int vel)
{
	g_pTarget = GetTargetPtr();
	g_pTarget->nVelocity = vel;
}
